import cn.hutool.core.util.StrUtil;
import cn.ren.hai.core.entity.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author aqian666
 * @ClassName: CodeGeneration
 * @Description: 代码生成器
 * @date 2019年7月2日14:08:37
 */
public class MybatisPlusNewGenerator {

    /**
     * 数据源配置
     */
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://106.53.250.134:3306/ren-hai?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=UTC&useSSL=false&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true",
            "root",
            "liLI980609@");

    @Test
    public void test() {
        Field[] declaredFields = BaseEntity.class.getDeclaredFields();
        String[] superColumnNames = new String[declaredFields.length];
        for (int i = 0; i < declaredFields.length; i++) {
            TableField field = declaredFields[i].getAnnotation(TableField.class);
            if (field != null && StrUtil.isNotEmpty(field.value())) {
                superColumnNames[i] = field.value();
            } else {
                superColumnNames[i] = declaredFields[i].getName();
            }

        }

        String path = "D:\\wangdao\\ren-hai\\base\\src\\main";



        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                // 全局配置
                .globalConfig((builder) -> builder
                        .author("王道")
                        .enableSwagger()
                        .disableOpenDir()
                        .outputDir(path+"\\java")
                        //.fileOverride()
                        .dateType(DateType.TIME_PACK)
                )
                // 包配置
                .packageConfig((builder) -> builder
                        .parent("cn.ren.hai.base")

                )

                // 策略配置
                .strategyConfig((builder) -> builder.addInclude(getTables("rh_table_field"))
                        //Controller
                        .controllerBuilder()
                        .enableRestStyle().enableHyphenStyle()
                        //Entity
                        .entityBuilder()
                        .superClass(BaseEntity.class)
                        .addSuperEntityColumns(superColumnNames)
                        .enableLombok()
                        .enableTableFieldAnnotation()
                        .versionColumnName("revision")
                        .versionPropertyName("revision")
                        .logicDeleteColumnName("del")
                        .logicDeletePropertyName("del")
                        //Server
                        .serviceBuilder()
                        //Mapper
                        .mapperBuilder()

                )

                /*
                    模板引擎配置，默认 Velocity 可选模板引擎 Beetl 或 Freemarker
                   .templateEngine(new BeetlTemplateEngine())
                 */
                .templateEngine(new FreemarkerTemplateEngine())
                .packageConfig((build)-> build
                        .pathInfo(Collections.singletonMap(OutputFile.mapperXml, path+"\\resources\\mapper"))
                )
                .execute();

    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }


}